home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / xlisp2.1 / xldist01.zoo / lsp / akavect.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1990-11-09  |  9.3 KB  |  304 lines

  1. ; This is AKALAH.LSP modified to use an array for the playing field rather
  2. ;  than a list.  The major result is a modest increase in speed because of
  3. ;  greatly reduced garbage collections
  4.  
  5. ; To play against the computer:
  6. ; (meplay #pits-per-side #pebbles-per-pit #search-depth #computer-plays-first)
  7. ;
  8. ; To have the computer play against itself:
  9. ; (play #pits-per-side #pebbles-per-pit #search-depth)
  10.  
  11.  
  12. ; The playing arena:
  13. ; 13    12 11 10 9  8  7
  14. ;    0  1  2  3  4  5     6 
  15.  
  16.  
  17. (defun makefield (length contents)
  18.     (let ((result (make-array length)))
  19.          (dotimes (i length) (setf (aref result i) contents))
  20.          result))
  21.  
  22. (setq *maxvalue* 1000)
  23. (setq *minvalue* (- *maxvalue*))
  24.  
  25. (defun copy (array) (generic array))    ; not the generic solution!
  26.  
  27. (defun prinb (x) (dotimes (i x) (princ #\space)))
  28.  
  29. (defun search (startpos depth whoseturn)
  30.    (if (zerop depth)
  31.        (list startpos (evaluate startpos whoseturn))
  32.        (let (bestval nextval bestpos succlist)
  33.         (setq succlist (successorsfn startpos whoseturn))
  34.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  35.         (setq 
  36.             beta    *maxvalue*
  37.             bestval *minvalue*
  38.         bestpos (car succlist))
  39.         (dolist (this succlist)
  40.             (when (wincheck this whoseturn)
  41.             (return-from search (list this *maxvalue*)))
  42.         (setq nextval (- (alphabeta this
  43.                         (- beta)
  44.                         (- bestval)
  45.                         (1- depth)
  46.                         (not whoseturn))))
  47.         (when (> nextval bestval)
  48.               (setq bestval nextval)
  49.               (setq bestpos this)))
  50.         (list bestpos bestval))))    ; return value
  51.  
  52. (defun alphabeta (position alpha beta depth whoseturn)
  53.     (if (zerop depth)
  54.         (evaluate position whoseturn)
  55.     (let (bestval nextval succlist)
  56.         (setq succlist (successorsfn position whoseturn))
  57.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  58.         (setq bestval alpha)
  59.         (dolist (this succlist)
  60.             (when (wincheck this whoseturn)
  61.               (return-from alphabeta *maxvalue*))
  62.         (setq nextval (- (alphabeta this
  63.                         (- beta)
  64.                         (- bestval)
  65.                         (1- depth)
  66.                         (not whoseturn))))
  67.         (when (> nextval bestval) (setq bestval nextval))
  68.         (when (<= beta bestval) (return-from alphabeta bestval)))
  69.         bestval)))
  70.  
  71. (defun successorsfn (position whoseturn 
  72.             &aux 
  73.             (picuphole (1- (if whoseturn *firstb* *firsta*)))
  74.             succlist 
  75.             succ 
  76.             stones 
  77.             disthole 
  78.             lasthole)
  79.    (dotimes (dummy *enda*)
  80.         (when (not (zerop (aref position (setq picuphole (1+ picuphole)))))
  81.           (setq    succ (copy position))
  82.           (setq stones (aref succ picuphole)) ; stones in this pit
  83.           (empty succ picuphole)
  84.           (setq disthole picuphole)
  85.           (dotimes (dummy2 stones) ; drop in successive holes except
  86.                          ; opponents kalah hole
  87.                  (setq disthole (nextdistholefn disthole whoseturn))
  88.                (dropin succ disthole 1))
  89.           (setq lasthole disthole)
  90.           (cond ((allownzerok succ whoseturn) ; all played out
  91.                (opptakesallfn succ whoseturn))
  92.             ((eq lasthole (kalaholefn whoseturn)) ; last in kalah
  93.              (setq succ (successorsfn succ whoseturn)))
  94.             ((and (eq (aref succ lasthole) 1) ; last into own empty
  95.                   (> (aref succ (opposholefn lasthole)) 0)
  96.                   (ownsidep lasthole whoseturn))
  97.              (dropin succ 
  98.                   (kalaholefn whoseturn)
  99.                  (1+ (aref succ (opposholefn lasthole))))
  100.              (empty succ lasthole)
  101.              (empty succ (opposholefn lasthole))
  102.              (when (allownzerok succ whoseturn)
  103.                    (opptakesallfn succ whoseturn))))
  104.           (setq succlist (nconc (preparelisfn succ) succlist))))
  105.     (if (null succlist)
  106.         (progn    (setq succ (copy position))
  107.         (opptakesallfn succ whoseturn)
  108.         (list succ))
  109.     succlist))
  110.  
  111. (defun dropin (position hole number) 
  112.     (setf (aref position hole) (+ number (aref position hole))))
  113.  
  114. (defun nextdistholefn (disthole whoseturn)
  115.     (cond    ((and whoseturn (eql disthole *lasta*)) *firstb*) ; skip own pile
  116.         ((and (not whoseturn) (eql disthole *lastb*)) *firsta*)
  117.         ((< disthole *endb*) (1+ disthole))
  118.         (t *firsta*)))
  119.  
  120. (defun empty (position hole)    ; empty out the given hole
  121.     (setf (aref position hole) 0))
  122.  
  123. (defun kalaholefn (whoseturn)   ; the scoring hole for the given player
  124.     (if whoseturn *endb* *enda*))
  125.  
  126. (defun opposholefn (hole)    ; calculate the opposing hole
  127.      (- *lastb* hole))
  128.  
  129. (defun ownsidep (hole whoseturn)
  130.     (if whoseturn (> hole *enda*) (< hole *firstb*)))
  131.  
  132. (defun preparelisfn (x)
  133.     (if (arrayp x)
  134.         (list x)
  135.         (unimbedfn x)))
  136.  
  137. (defun reorder (poslist whoseturn)
  138.     (sortdown poslist (statvalsfn poslist whoseturn)))
  139.  
  140.  
  141. (defun sortdown (poslist statvalvec)
  142.     (if (null (cdr poslist))
  143.         poslist
  144.         (let ((maxindex (indexfn statvalvec)))
  145.              (cons (nth maxindex poslist)
  146.                (sortdown (deletel maxindex poslist)
  147.                         (deletel maxindex statvalvec))))))
  148.  
  149. (defun deletel (index list)    ; delete the index'th value in list
  150.     (if (zerop index)
  151.         (cdr list)
  152.         (prog2
  153.           (rplacd (nthcdr (1- index) list) (nthcdr (1+ index) list))
  154.           list)))
  155.  
  156. (defun indexfn (x &aux result)    ; find position of maximum value
  157.     (do    ((list x (cdr list))
  158.          (n    0 (1+ n))
  159.          (val  *minvalue* (cond ((> (car list) val) 
  160.                      (setq result n)
  161.                     (car list))
  162.                     (t val))))
  163.         ((null list) result)))
  164.  
  165. (defun statvalsfn (poslist whoseturn)
  166.     (if (null poslist)
  167.         'nil
  168.         (cons (evaluate (car poslist) whoseturn)
  169.               (statvalsfn (cdr poslist) whoseturn))))
  170.  
  171. (defun wincheck (position whoseturn)
  172.     (> (aref position (kalaholefn whoseturn)) *halfall*))
  173.  
  174. (defun evaluate (position whoseturn) ; assign the value to the position
  175.                      ; could obviously use work
  176.     (let ((ownkala (aref position (kalaholefn whoseturn)))
  177.           (oppkala (aref position (kalaholefn (not whoseturn)))))
  178.          (cond ((> ownkala *halfall*) *maxvalue*)
  179.                 ((> oppkala *halfall*) *minvalue*)
  180.            (t (- ownkala oppkala)))))
  181.  
  182. (defun printpos (position)
  183.          (terpri)
  184.          (prin1 (aref position *enda*))
  185.          (prinb (if (> 10 (aref position *enda*)) 3 2))
  186.          (dotimes (n *enda*)
  187.               (let ((val (aref position (- *enda* n 1))))
  188.              (prin1 val)
  189.             (prinb (if (> 10 val) 2 1))))
  190.          (terpri)
  191.          (prinb 4)
  192.          (dotimes (n *enda*)
  193.              (let ((val (aref position (+ *firstb* n))))
  194.              (prin1 val)
  195.             (prinb (if (> 10 val) 2 1))))
  196.          (prinb 2)
  197.          (prin1 (aref position *endb*))
  198.          (terpri)
  199.          (terpri))
  200.  
  201. ; sum the players pieces yet to play
  202. (defun countown (position whoseturn)
  203.     (let ((start (if whoseturn *firstb* *firsta*))
  204.           (sum 0))
  205.          (dotimes (n *enda*) 
  206.                    (setf sum (+ sum (aref position (+ n start)))))
  207.          sum))
  208.  
  209. ; check if player is out of pieces (then opponent will take remainder)
  210. (defun allownzerok (position whoseturn) 
  211.     (zerop (countown position whoseturn)))
  212.  
  213. (defun opptakesallfn (position whoseturn)
  214.     (dropin position
  215.         (kalaholefn (not whoseturn))
  216.         (countown position (not whoseturn)))
  217.     (do    ((count *enda* (1- count))
  218.          (hole (if whoseturn *firstb* *firsta*) (1+ hole)))
  219.         ((zerop count))
  220.         (empty position hole)))
  221.  
  222. (defun nextmove (depth whoseturn)
  223.     (terpri)
  224.     (setq *board* (search (car *board*) depth whoseturn))
  225.     (printpos (car *board*))
  226.     (print (cdr *board*))
  227.     (terpri))
  228.  
  229. ; (defun unimbedfn (poslist)
  230. ;    (cond    ((null poslist) 'nil)
  231. ;        ((atom (caar poslist))
  232. ;         (cons (car poslist) (unimbedfn (cdr poslist))))
  233. ;        (t (append (unimbedfn (car poslist))
  234. ;               (unimbedfn (cdr poslist))))))
  235.  
  236. (defun unimbedfn (poslist)
  237.     (do    ((list poslist (cdr list))
  238.          (result nil (if (arrayp (car list))
  239.                   (cons (car list) result)
  240.                  (append (unimbedfn (car list)) result))))
  241.             ((null list) result)))
  242.  
  243. (defun initialize (holes pebbles &aux temp) ; initialize the playing area
  244.     (setq *enda* holes)
  245.     (setq *endb* (+ holes holes 1))
  246.     (setq *firsta* 0)
  247.     (setq *lasta* (1- *enda* ))
  248.     (setq *firstb* (1+ *enda*))
  249.     (setq *lastb* (1- *endb*))
  250.     (setq *halfall* (* holes pebbles))
  251.     (setq temp (makefield (+ (* 2 holes) 2) pebbles))
  252.     (setf (aref temp *enda*) 0)
  253.     (setf (aref temp *endb*) 0)
  254.     (setq *board* (list temp 0)))
  255.  
  256. (defun play (holes pebbles depth) ; play the game
  257.     (initialize holes pebbles)
  258.     (do    ((whoseturn nil (not whoseturn)))
  259.         ((or (eql (cadr *board*) *maxvalue*)
  260.              (eql (cadr *board*) *minvalue*)))
  261.         (nextmove depth whoseturn)))
  262.  
  263. (defun meplay (holes pebbles depth computer-first) 
  264.   (prog (picuphole)
  265.     (initialize holes pebbles)
  266.     (when computer-first (setq succ (copy (car *board*)))
  267.           (go y))
  268. n    (setq succ (copy (car *board*)))
  269.     (printpos succ)
  270.     (if (> (aref succ *enda*) *halfall*) (return t))   ; win for side a
  271.     (if (> (aref succ *endb*) *halfall*) (return nil)) ; win for side b
  272. x    (princ "Hole? ") (setq picuphole (read))
  273.     (if (or (not (numberp picuphole))
  274.         (> picuphole *firstb*)
  275.         (> 1 picuphole)
  276.         (zerop (setq stones (aref succ (setq picuphole (1- picuphole))))))
  277.         (go x))
  278.     (empty succ picuphole)
  279.     (setq disthole picuphole)
  280.     (dotimes (dummy stones)
  281.              (dropin succ (setq disthole (nextdistholefn disthole nil)) 1))
  282.     (setq lasthole disthole)
  283.     (cond    ((allownzerok succ nil)
  284.          (opptakesallfn succ nil)
  285.          (setq *board* (list succ 0))
  286.          (go n))
  287.         ((eql lasthole *enda*)
  288.          (setq *board* (list succ 0))
  289.          (go n))
  290.         ((and (eql (nth lasthole succ) 1)
  291.               (> (aref succ (opposholefn lasthole)) 0)
  292.               (> *enda* lasthole))
  293.          (dropin succ *enda* (1+ (aref succ(opposholefn lasthole))))
  294.          (empty succ lasthole)
  295.          (empty succ (opposholefn lasthole))
  296.          (when (allownzerok succ nil)
  297.                (opptakesallfn succ nil)
  298.                (setq *board* (list succ 0))
  299.                (go n))))
  300.     (printpos succ)
  301. y    (setq *board* (search succ depth t))
  302.     (go n)))
  303.  
  304.